package colegios

class ReligionController {
	def religionService
	
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [religionInstanceList: Religion.list(params), religionInstanceTotal: Religion.count()]
    }

    def create = {
        def religionInstance = new Religion()
        religionInstance.properties = params
        return [religionInstance: religionInstance]
    }

    def save = {
        def religionInstance = new Religion(params)
        if (religionService.saveObj(religionInstance)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'religion.label', default: 'Religion'), religionInstance.id])}"
            redirect(action: "show", id: religionInstance.id)
        }
        else {
            render(view: "create", model: [religionInstance: religionInstance])
        }
    }

    def show = {
        def religionInstance = religionService.showObj(new Integer(params.id))
        if (!religionInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'religion.label', default: 'Religion'), params.id])}"
            redirect(action: "list")
        }
        else {
            [religionInstance: religionInstance]
        }
    }

    def edit = {
        def religionInstance = religionService.showObj(new Integer(params.id))
        if (!religionInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'religion.label', default: 'Religion'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [religionInstance: religionInstance]
        }
    }

    def update = {
        def religionInstance = religionService.showObj(new Integer(params.id))
        if (religionInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (religionInstance.version > version) {
                    
                    religionInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'religion.label', default: 'Religion')] as Object[], "Another user has updated this Religion while you were editing")
                    render(view: "edit", model: [religionInstance: religionInstance])
                    return
                }
            }
            religionInstance.properties = params
            if (!religionInstance.hasErrors() && religionService.saveObj(religionInstance)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'religion.label', default: 'Religion'), religionInstance.id])}"
                redirect(action: "show", id: religionInstance.id)
            }
            else {
                render(view: "edit", model: [religionInstance: religionInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'religion.label', default: 'Religion'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def religionInstance = religionService.showObj(new Integer(params.id))
        if (religionInstance) {
            try {
				religionService.deleteObj(religionInstance)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'religion.label', default: 'Religion'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'religion.label', default: 'Religion'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'religion.label', default: 'Religion'), params.id])}"
            redirect(action: "list")
        }
    }
}
